package com.lang.controller;

import com.lang.entity.Address;
import com.lang.entity.Cart;
import com.lang.entity.Orders;
import com.lang.entity.User;
import com.lang.service.AddressService;
import com.lang.service.CartService;
import com.lang.service.OrderService;
import com.lang.service.impl.AddressServiceImpl;
import com.lang.service.impl.CartServiceImpl;
import com.lang.service.impl.OrderServiceImpl;
import com.lang.utils.RandomUtils;
import com.lang.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/*
 * @Date : 2024/3/14 10:45
 * @Created by Lang
 * @ProjectName : LclShop
 */
@WebServlet("/order")
public class OrderServlet extends BaseServlet {
    private CartService cartService = new CartServiceImpl();
    private AddressService addressService = new AddressServiceImpl();
    private OrderService orderService = new OrderServiceImpl();

    public String getOrderView(HttpServletRequest request, HttpServletResponse response) {

        User user = (User) request.getSession().getAttribute(StrUtils.lOG_USER);
        List<Cart> carts = cartService.getCarts(user.getU_id());
        List<Address> addresses = addressService.getAddress(user.getU_id());
        request.setAttribute("cartList", carts);
        request.setAttribute("addList", addresses);
        return StrUtils.FORWARD + "/order.jsp";
    }

    public String addOrder(HttpServletRequest request, HttpServletResponse response) {
        String aid = request.getParameter("aid");
        String sum = request.getParameter("sum");
        User user = (User) request.getSession().getAttribute(StrUtils.lOG_USER);
        orderService.addOrder(aid, sum, user.getU_id());

        return StrUtils.FORWARD + "/order?action=showOrder";
    }

    public String showOrder(HttpServletRequest request, HttpServletResponse response) {
        User user = (User) request.getSession().getAttribute(StrUtils.lOG_USER);
        List<Orders> list = orderService.showOrder(user.getU_id());
        request.setAttribute("orderList",list);
        return StrUtils.FORWARD+"/orderList.jsp";
    }

    public String getOrderDetail(HttpServletRequest request,HttpServletResponse response){

        String oid = request.getParameter("oid");

        Orders orders = orderService.showItems(oid); //根据oid得到订单对象
        request.setAttribute("od",orders);
        System.out.println(orders);
        return StrUtils.FORWARD+"/orderDetail.jsp";
    }
}
